"""
下载的图片需要生成json文件 放入hadoop平台
"""
import json

from logicaltool.BaseTools.file_tools import FileTools
from xjlibrary.mdatetime.mtime import getTodayDate
from xjlibrary.our_file_dir import BaseDir


class FileToJson(FileTools):

    def __init__(self):
        super().__init__()

    def get_all_file(self, cover_dir_path):
        count = 0
        for filePath in BaseDir.get_dir_all_files(cover_dir_path):
            stri = "/smartlib/cnipapatent"
            stri += filePath.replace(cover_dir_path, "").replace("\\", "/")
            pub_no = filePath.split("\\")[-1]
            pub_no = pub_no.replace(".jpg", "").strip()
            dicts = {"pub_no": pub_no, "cover": stri}
            jsonmsg = json.dumps(dicts, ensure_ascii=False)
            self.inputobj1.add(jsonmsg)
            count += 1
            if count % 10000 == 1:
                print(count)

    def write_file(self):
        filepath = getTodayDate() + "_jsoncover.txt"
        BaseDir.single_write_file("\n".join(self.inputobj1), filepath)


if __name__ == "__main__":
    filejson = FileToJson()
    filejson.get_all_file(r"\\192.168.30.177\样例数据\cnipapatent\cnipapatent_cover_raw_20181129")
    print("写入文件")
    filejson.write_file()
